/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * usermanagement.java
 *
 * Created on May 23, 2011, 7:25:08 PM
 */
package Project.GUI;

import Project.DBHelper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tvtrung
 */
public class usermanagement extends javax.swing.JFrame {

    Vector vTitle, vContent;

    /** Creates new form usermanagement */
    public usermanagement() {
        initComponents();
        innitable();
        setLocationRelativeTo(this);
    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:JDBC", "sa", "123");
        return con;
    }

    public void innitable() {
        vTitle = new Vector();
        vContent = new Vector();
        try {
            ///  vContent.clear();
            //Connection cn = DBHelper.getConnection();
            Connection cn = getConnection();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("select * from Users");
            ResultSetMetaData t = rs.getMetaData();
            for (int i = 1; i <= t.getColumnCount(); i++) {
                vTitle.add(t.getColumnName(i));
            }
            while (rs.next()) {
                Vector team = new Vector();
                team.addElement(Integer.toString(rs.getInt("UserID")));
                team.addElement(rs.getString("UserName"));
                team.addElement(rs.getString("PassWord"));
                team.addElement(rs.getString("Per"));
                team.addElement(rs.getString("Name"));
                team.addElement(rs.getString("Email"));

                vContent.addElement(team);
                team = null;
            }
            DefaultTableModel jt = new DefaultTableModel(vContent, vTitle);
            jtable.setModel(jt);
            rs.close();
            st.close();
            cn.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(usermanagement.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(usermanagement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ManagementUser");

        jtable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        jScrollPane1.setViewportView(jtable);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/closer.gif"))); // NOI18N
        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/imagesz.jpeg"))); // NOI18N
        jButton2.setText("DelUser");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/index.jpeg"))); // NOI18N
        jButton3.setText("AddUser");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/xc.jpeg"))); // NOI18N
        jButton4.setText("Update");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(jButton3)
                .addGap(75, 75, 75)
                .addComponent(jButton2)
                .addGap(86, 86, 86)
                .addComponent(jButton4)
                .addGap(84, 84, 84)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton2)
                    .addComponent(jButton4)
                    .addComponent(jButton1))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        new AddnewUser(this, true);        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if (jtable.getSelectedRow() != -1) {
            new UpdateUser(this, true, (Vector) vContent.get(jtable.getSelectedRow()));
        } else {
            JOptionPane.showMessageDialog(null, "You must choose User", "Note", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (jtable.getSelectedRow() != -1) {
            int index = Integer.parseInt(jtable.getValueAt(jtable.getSelectedRow(), 0).toString());
            try {
                String sql_del = "Delete from Users where UserID=" + index;
              //  Connection cn = DBHelper.getConnection();
                Connection cn= getConnection();
                PreparedStatement pst = cn.prepareStatement(sql_del);

                
                int i = pst.executeUpdate();
                System.out.println(i);
                if (i == 1) {
                    JOptionPane.showMessageDialog(null, "Deleted!", "Note", JOptionPane.INFORMATION_MESSAGE);
                    innitable();
                }
            } catch (Exception e) {
            }
        } else {
            JOptionPane.showMessageDialog(null, "You must choose User", "Note", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
        Login l = new Login();
        l.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new usermanagement().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtable;
    // End of variables declaration//GEN-END:variables
}
